<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: post.php Sat Jul 18 12:39:04 CST 2009
 */

include "conf.inc";

if ( !is_object($xoopsUser) ){
	redirect_header("/user.php",5,_NOPERM);
}

$op = isset($_GET["op"]) ? $_GET["op"] : "create";
$group_id = isset($_GET["gid"]) ? intval($_GET["gid"]) : 0;

$groups_handler = xoops_getmodulehandler("groups");
$posts_handler = xoops_getmodulehandler("posts");
$category_handler = xoops_getmodulehandler("category");

$groups_obj = $groups_handler->get($group_id);
if ( !is_object($groups_obj) || $groups_obj->isNew() ) {
    redirect_header("index.php");
}
//group assess permission
if ( false === getUserAccessPermission($groups_obj) ) {
	header("Location: "._GROUPS_URL."/groups.php?gid={$group_id}");
}
// set user footprint
$footprint_handler = xoops_getmodulehandler("footprint");
$_v = $footprint_handler->setFootprint($group_id);
if ( $_v == 1 ) {
    /** 产生一个事件 **/
	$events_handler->eventApi($xoopsUser->getVar("uid"),$group_id,108,$groups_obj->getVar("group_name"),$group_id,"");
}

$groupinfo = $groups_obj->getValues();

/** 群组菜单导航 **/
$_links = $groups_handler->getLinkToGroupMenu($group_id);
$groupinfo = array_merge($groupinfo,$_links);
$groupinfo["group_logo"] = $groups_obj->groupLogo();
$groupinfo["groupstatus"]=$groups_obj->groupStatus();
$_uid = ( !empty($xoopsUser) && is_object($xoopsUser) ) ? $xoopsUser->getVar("uid") : 0;
$groupinfo["group_join"] = ( false === $groups_obj->isCreateUser($_uid) ) ? $groups_obj->getGroupJoinLink($_uid) : "";

$xoopsOption["xoops_pagetitle"] = $xoopsModuleConfig["groupstitle"] .$groups_obj->getVar("group_name"). "- "._GP_POST_TOPIC;
$xoopsOption["template_main"] = "groups_group_post.html";
include "header.php";

$isPost = false;
if ( $op == "edit" || $op == "create" ) {
    $isPost = ( true === getUserTopicPermission($groups_obj) ) ? true : false;
}
if ( $op == "quote" || $op == "reply" ) {
    $isPost = ( true === getUserReplyPermission($groups_obj) ) ? true : false;
}

switch ($op) {
    case "edit":
        $post_id = isset($_GET["post_id"]) ? intval($_GET["post_id"]) : 0;
        $post_obj = $posts_handler->get($post_id);
        if ( !is_object($post_obj) || $post_obj->isNew() ) {
            redirect_header(_GROUPS_URL);
        }
        $isNewTopic = $post_obj->isNew();
        
        if ( ( $post_obj->getVar("uid") != $xoopsUser->getVar("uid")  || $post_obj->getVar("post_dateline") <= (time() - 3600) )  &&  false === getIsGroupsAdmin($group_id,$xoopsUser->getVar("uid")  )) {
        	redirect_header("viewthread.php?tid=".$post_obj->getVar("tid"), 5, _GP_TOPIC_NOPERM);
        }
        
        if ( $post_obj->getVar("post_attachments") > 0 ) {
            $attachments_handler = xoops_getmodulehandler("attachments");
        	$att_list = $attachments_handler->getAllAttachmentList($post_id);
        }
        $first = $post_obj->getVar("first") ;
        $tid = $post_obj->getVar("tid") ?  $post_obj->getVar("tid") : 0;
        $postitem = $post_obj->getValues();
        $postitem["post_content"] = $post_obj->getVar("post_content","e");
    break;
    
    case "quote":
        $post_id = isset($_GET["post_id"]) ? intval($_GET["post_id"]) : 0;
        $post_obj = $posts_handler->get($post_id);
        if ( !is_object($post_obj) || $post_obj->isNew() ) {
            redirect_header(_GROUPS_URL);
        }
        $group_id = $post_obj->getVar("group_id");
        $groups_obj = $groups_handler->get($group_id);
        
        $first = 0;
        $tid = $post_obj->getVar("tid") ?  $post_obj->getVar("tid") : 0;
        $isNewTopic = true;
        $postitem = $post_obj->getValues();
        $postitem["post_content"] = $post_obj->getVar("post_content","e");
        $postitem["post_subject"] = "Re:".$postitem["post_subject"];
        $postitem["post_content"] = "[quote]" . $postitem["post_content"] ."[/quote]";
    break;
    
    case "reply":
        $tid = isset($_GET["tid"]) ? intval($_GET["tid"]) : 0;
        
        $threads_handler = xoops_getmodulehandler("threads");
        $threads_obj = $threads_handler->get($tid);
        
        if ( !is_object($threads_obj) || $threads_obj->isNew() ) {
            redirect_header(_GROUPS_URL);
        }
        
        $post_obj = $posts_handler->get();
        $isNewTopic = $post_obj->isNew();
        
        $first = 0 ;
        $postitem = $post_obj->getValues();
        $postitem["post_subject"] = "Re:".$threads_obj->getVar("threads_subject");
    break;
    
    case "create":
    default:
        $post_obj = $posts_handler->get();
        $isNewTopic = $post_obj->isNew();
        $first = 1;
        $postitem = $post_obj->getValues();
    break;
}


// uploads from
if ( !empty($postitem) ) {
    if ( $isPost ) {
        include_once(dirname(__FILE__)."/include/form.post.topic.php");
    }
    if ( !empty($att_list) ) {
        $xoopsTpl->assign("att_list",$att_list);
    }
}

$xoopsTpl->assign(array("groupinfo"=>$groupinfo,"op"=>"forum","isPost"=>$isPost));
include "footer.php";
?>